<?php
$folder="./data";//.\\data
$fp=opendir($folder);
$mysqli = new mysqli('localhost', 'root', '123456', 'report');

if ( mysqli_connect_errno ()) {
     printf ( "Connect failed: %s\n" ,  mysqli_connect_error ());
    exit();
}
$mysqli->query("set names utf8");
$a = file('./list.txt');
foreach($a as $line => $content){
    $array = split("\t", $content);
    echo $array[1]." ".$array[2]." ".$array[3]."<br>";
    $gender = 0;
    if($array[1] == '男')
        $gender=1;
    else if($array[1] == '女')
        $gender=2;
    
    $str = file_get_contents("./data/".$array[3].$array[2].".html");//$str = file_get_contents(".\\data\\".$keys[$per+3].$keys[$per+2].".html");
    preg_match_all ("/<span\s{1}id=\"lbl(.+)\">(.+)<\/span>/isU",
            $str,$out ,  PREG_PATTERN_ORDER );
    $user = array();
    $user = $out[2];
//    print_r($user);
    $queryreport  =  "INSERT INTO reportinfo(username,gender,birthday,company,examtime,password,"
            . "examid,examsubid,idcard) VALUES ('".$user[0]."','".$gender."','".date("Y-m-d",strtotime($array[5]))."','".$user[3]
            ."','".date("Y-m-d H:i:s",strtotime($user[4]))."','".$user[5]."','".$user[6]."','".$array[3]."','"
            .$user[7]."')" ;
    $mysqli -> query ( $queryreport );
    $reportid = $mysqli->insert_id;
    preg_match_all ("/<span\s{1}id=\"Repeater1_ct(.*)MaterielName\">(.+)<\/span>/isU",
            $str,$programname ,  PREG_PATTERN_ORDER );
    preg_match_all ("/<td\s{1}align=\"right\"\s{1}class=\"brown02_14\"\s{1}style=\"color:#ED9C72\">(.+)<\/td>/isU",
            $str,$programdoctor ,  PREG_PATTERN_ORDER );
    $programlen = count($programname[2]);
    $programarray = array();
    for($i=0;$i<$programlen;$i++)
    {
        $queryprogram  =  "INSERT INTO programinfo(reportid,programname,doctorname)"
                . "VALUES (".$reportid.",'".$programname[2][$i]."','".substr($programdoctor[1][$i],9)."')" ;
        //echo $programname[2][$i]."','".$programdoctor[1][$i]."<br>";
        $mysqli -> query ( $queryprogram );
        $programid = $mysqli->insert_id;
        $programarray[$i] = $programid;
        
    }
    preg_match_all ("/<table\s{1}width=\"100%\"\s{1}border=\"0\"\s{1}cellpadding=\"4\"\s{1}cellspacing=\"0\">(.+)<\/table>/isU",
            $str,$item ,  PREG_PATTERN_ORDER );
    $items = $item[1];;
    for($k=1;$k<=$programlen;$k++)
    {
        preg_match_all ("/<td\s{1}align=\"left\"\s{1}class=\"brown02_12\">(.+)<\/td>/isU",
                $items[$k] ,$itemout0,  PREG_PATTERN_ORDER );
        preg_match_all ("/<td\s{1}align=\"left\"\s{1}class=\"black_12\">(.*)<\/td>/isU",
                $items[$k] ,$itemout1,  PREG_PATTERN_ORDER );
        preg_match_all ("/<td\s{1}align=\"right\"\s{1}class=\"black_12\">(.*)<\/td>/isU",
                $items[$k] ,$itemout2,  PREG_PATTERN_ORDER );
        $small_item_count = count($itemout0[1]);
        $jtemp = 0;
        //$jtemp2 = 1;
        for($j=0;$j<$small_item_count;$j++){
            $re = $itemout1[1][$jtemp];
            $isexception1 = strpos($re,'↑');
            $isexception2 = strpos($re,'↓');
            $queryitem  =  "INSERT INTO iteminfo(reportid,programid,itemname,result,unit,normal) "
                    . "VALUES ('".$reportid."','".$programarray[$k-1]."','".$itemout0[1][$j]."','".$itemout1[1][$jtemp]."','"
                    .$itemout1[1][$jtemp+1]."','".$itemout2[1][$j]."')" ;
            if($isexception1)
            {
                $queryitem  =  "INSERT INTO iteminfo(reportid,programid,itemname,result,unit,normal,issue) "
                    . "VALUES ('".$reportid."','".$programarray[$k-1]."','".$itemout0[1][$j]."','".$itemout1[1][$jtemp]."','"
                    .$itemout1[1][$jtemp+1]."','".$itemout2[1][$j]."','↑')" ;
                
            }
            if($isexception2)
            {
                $queryitem  =  "INSERT INTO iteminfo(reportid,programid,itemname,result,unit,normal,issue) "
                    . "VALUES ('".$reportid."','".$programarray[$k-1]."','".$itemout0[1][$j]."','".$itemout1[1][$jtemp]."','"
                    .$itemout1[1][$jtemp+1]."','".$itemout2[1][$j]."','↓')" ;
                
            }
           // echo $itemout0[1][$j]."','".$itemout1[1][$jtemp].'  '.$itemout1[1][$jtemp+1]."<br>";
            $mysqli -> query ( $queryitem );
            $jtemp=$jtemp+2;
          //  $jtemp2=$jtemp2+2;
            
        }
        
        }    

}